package com.glorystone.ins.service.impl;


import com.glorystone.ins.dao.AdvanceMapper;
import com.glorystone.ins.domain.Advance;
import com.glorystone.ins.domain.SysUser;
import com.glorystone.ins.service.AdvanceService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;


@Service
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class AdvanceServiceImpl implements AdvanceService {

    @Autowired
    private AdvanceMapper advanceMapper;

    @Transactional(readOnly = false, rollbackFor = Exception.class)
    @Override
    public int insertApply(Advance advance) {
        return advanceMapper.insertApply(advance);
    }

    @Transactional(readOnly = false, rollbackFor = Exception.class)
    @Override
    public List<Advance> findSuccessStatus() {
        return advanceMapper.findSuccessStatus();
    }

    @Override
    public List<Advance> findAdvanceList() {
        return advanceMapper.findAdvanceList();
    }
    @Override
    public Advance findAdvanceOne(Long tid) {
        return advanceMapper.findAdvanceOne(tid);
    }

    @Override
    public List<Advance> findAdvanceAmt() {
        return advanceMapper.findAdvanceAmt();
    }

    @Override
    public Advance findTodayAdvance() {
        List<Advance> list = advanceMapper.findTodayAdvance();
        Advance advance = new Advance();
        if(list.size()<1){
            advance.setApplyAmount(new BigDecimal(0));
            advance.setActualAmount(new BigDecimal(0));
        }else{
            advance = list.get(0);
        }
        return advance;
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public boolean upActualAmount(BigDecimal actualAmount) {
        List<Advance> list = advanceMapper.findTodayAdvance();
        Advance advance = new Advance();
        if(list.size()>0){
            Advance advanceOld = list.get(0);
            BigDecimal actualAmt = advanceOld.getActualAmount();
            if(actualAmt == null){
                actualAmt = new BigDecimal(0);
            }
            SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
            actualAmt = actualAmt.add(actualAmount);
            advance.setTid(advanceOld.getTid());
            advance.setActualAmount(actualAmt);
            advance.setUpdateAt(LocalDateTime.now());
            advance.setUpdateBy(user.getTid());
            int i = advanceMapper.checkAdvance(advance);
            return i==0 ? false : true;
        }
        return false;
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void insertTodatAmount(Advance advance) {
        advanceMapper.insertTodatAmout(advance);
    }

    /**
     * 业务负责人审核
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void checkAdvance(Advance advance) {
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        advance.setUpdateAt(LocalDateTime.now());
        advance.setUpdateBy(user.getTid());
        advanceMapper.checkAdvance(advance);

    }

    /**
     * 财务经理审核
     */
    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public void checkAdvances(Advance advance) {
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        Advance advance1=advanceMapper.selectByTid(advance.getTid());
        Advance advance2=advanceMapper.findAmount();
        advanceMapper.updateStatus();
        advance.setUpdateAt(LocalDateTime.now());
        advance.setUpdateBy(user.getTid());
        advance.setAuditStatus(Advance.AuditStatusEnum.CHECK_SUCCESS.getValue());
        advance.setStatus(1);
        advance.setTodatAmount(advance1.getApplyAmount());
        BigDecimal act=advance2.getActualAmount();
        if(act == null){
            act = new BigDecimal(0);
        }
        advance.setActualAmount(act);
        advanceMapper.checkAdvance(advance);
    }

    /**
     * 审核驳回
     */
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    @Override
    public void rejectAdvance(Advance advance) {
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        Advance advances = advanceMapper.selectByPrimaryKey(advance.getTid());
        advances.setUpdateAt(LocalDateTime.now());
        advances.setUpdateBy(user.getTid());
        advances.setAuditStatus(Advance.AuditStatusEnum.CHECK_STATUS_REJECT.getValue());
        advances.setStatus(2);
        advanceMapper.updateByPrimaryKey(advances);
    }

    @Override
    public List<Advance> getAdvanceList(String type) {
        String day = ""+7;
        if("2".equals(type)){
            day = ""+30;
        }else if("3".equals(type)){
            day = ""+365;
        }
       List<Advance> list=advanceMapper.getAdvanceList(day);
        return list;
    }

    @Transactional(readOnly = false, rollbackFor = Exception.class)
    @Override
    public void updateStatus() {
        advanceMapper.updateStatus();
    }

    @Transactional(readOnly = false, rollbackFor = Exception.class)
    @Override
    public void clearData() {
        advanceMapper.clearData();
    }

    @Override
    public Advance findClearData() {
        return advanceMapper.findClearData();
    }


    @Transactional(readOnly = false, rollbackFor = Exception.class)
    @Override
    public void saveAmount(Advance advance) {
        advanceMapper.saveAmount(advance);
    }
}

